﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Data;
using System.Configuration;

namespace ath2011_sql_WPF
{
    /// <summary>
    /// Interaction logic for r1.xaml
    /// </summary>
    public partial class r1 : Window
    {    
        bool hasGen;


        public r1(string[] str1, string conStr, string cmd1,int type)
        {

            InitializeComponent();

            for (int i = 0; i < str1.Length; i++)
            {
                txtResults.Text += str1[i] + "\r\n";

            }

            ShowResult2(conStr, cmd1,type);

        }

        private void r1_Load(object sender, EventArgs e)
        {

        }

        private void txtResults_TextChanged(object sender, EventArgs e)
        {

        }
        
        

        public void ShowResult2(string conStr, string cmd1, int type)
        {
            int rowsAffected = 0;
            hasGen = false;
            if (type == 0)
            {
                try
                {

                    SqlConnection con = new SqlConnection(conStr);

                    SqlDataAdapter sda = new SqlDataAdapter(cmd1, conStr);
                    sda.SelectCommand = new SqlCommand(cmd1, con);
                    DataTable dt = new DataTable("tb1");

                    sda.Fill(dt);

                    dg.ItemsSource = dt.DefaultView;
                    dg.UpdateLayout();
                    rowsAffected = dg.Items.Count;
                    //dg.Sel
                    //dg.ClearSelection();
                    //  dg.Rows[tableNamesBS.Position].Selected = true;
                    hasGen = true;
                    dgUpdate();
                }
                catch { txtResults.Text += "Cannot display in DataGrid " + "\r\n"; }

                txtRows.Content = "Number of Rows: " + rowsAffected;
            }
            else
            {
                SqlConnection myConn = new SqlConnection(conStr);
                string str = cmd1;


                myConn.Open();


                SqlCommand myCommand = new SqlCommand(str, myConn);
                myCommand.ExecuteNonQuery();

                myConn.Close();

            }
        }
        private void dgUpdate()
        {
          //  dg.ClearSelection();
            dg.SelectedCells.Clear();
           // dg.SelectedCells.Add(tableNamesBS.Position);
           // dg.Rows[tableNamesBS.Position].Selected = true;
          //  txtRows.Content = tableNamesBS.Position + 1 + " out of " + (dg.RowCount - 1);
            //  records();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (hasGen)
            {
             //   tableNamesBS.MovePrevious();
               // dg.SelectedCells.
                dgUpdate();
            }
        }

        private void generuj_kod_Click(object sender, EventArgs e)
        {
            if (hasGen)
            {
             //   tableNamesBS.MoveNext();
               
                dgUpdate();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (hasGen)
            {
              //  tableNamesBS.MoveFirst();
                dg.SelectedCells.First();
                dgUpdate();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (hasGen)
            {
              //  tableNamesBS.MoveLast();
                dgUpdate();
            }
        }
    }
}
